﻿package com.snzflash.components.display 
{
	import com.snzflash.components.core.UIComponent;
	import com.snzflash.components.grid.Grid;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.IBitmapDrawable;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * Набор плиток, используется для заполнения фоном однотипным рисунком
	 * 
	 * плитку можно задать как URL, или DisplayObject
	 * @author firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class TilesSet extends UIComponent
	{
		
		private var _tile:Bitmap;
		private var _tileLoader:Loader;
		private var _tilesGrid:Grid;
		private var _tilesArray:/*DisplayObject*/Array;
		
		
		public function TilesSet() 
		{
			super();
		};
		
		override protected function create():void 
		{
			super.create();
			_tileLoader = new Loader();
			_tileLoader.contentLoaderInfo.addEventListener ( Event.COMPLETE, onTileImageComplete );
			_tileLoader.contentLoaderInfo.addEventListener ( IOErrorEvent.IO_ERROR, onTileImageError );
			
			_tilesGrid = new Grid();
			_tilesGrid.itemRenderer = TileRenderer;
			addChild(_tilesGrid);
		};
		
		override public function destroy():void 
		{
			super.destroy();
			_tileLoader.contentLoaderInfo.removeEventListener ( Event.COMPLETE, onTileImageComplete );
			_tileLoader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR, onTileImageError );
			
			try {_tileLoader.close();}
			catch (err:Error) { }
			
			try {_tileLoader.unload();}
			catch (err:Error) { }
			_tileLoader = null;
		};
		
		override protected function updateDisplayList():void 
		{
			super.updateDisplayList();
			////trace(width, height);
			scrollRect = new Rectangle(0, 0, width, height);
			_tilesGrid.width = width;
			_tilesGrid.height = height;
			
			if (!_tile || width == 0 || height == 0) return;
			
			 //создаем нужное колво плиток
			var num_rows:int = Math.ceil(height / _tile.height);
			var num_columns:int = Math.ceil(width / _tile.width);
			
			////trace(num_rows, num_columns);
			
			var total:int = num_columns * num_rows;
			var dp:Array = [];
			var data:Bitmap;
			_tilesGrid.numColumns = num_columns;
			_tilesGrid.numRows = num_rows;
			////trace(total);
			
			for (var i:int = 0; i < total; i++) 
			{
				data = new Bitmap(_tile.bitmapData.clone());
				dp.push( data );
			};
			_tilesGrid.dataProvider = dp;
		};
		
		public function loadTileImage(url:String):void 
		{
			////trace(url);
			_tileLoader.load ( new URLRequest ( url ) );
		};
		
		public function get tile():Bitmap { return _tile; };
		
		public function set tile(value:Bitmap):void 
		{
			_tile = value;
			//_tilesGrid.itemRenderer
			_systemManager.invalidate();
		};
		
		protected function onTileImageError(event:IOErrorEvent):void 
		{
			
		};
		
		protected function onTileImageComplete(event:Event):void 
		{
			////trace(this);
			var bitmap_data:BitmapData = new BitmapData(_tileLoader.content.width, _tileLoader.content.height);
			bitmap_data.draw ( _tileLoader.content );
			
			tile = new Bitmap(bitmap_data);
			
			//var bitmap:Bitmap = new Bitmap(bitmap_data);
			//bitmap.bitmapData.clone
			//addChild(bitmap);
			//var bitmap:Bitmap = IBitmapDrawable ( _tileLoader.content );
			
			
		};
	}

}
import com.snzflash.components.list.ListItemRenderer;
import flash.display.Bitmap;

class TileRenderer extends ListItemRenderer
{
	private var _content:Bitmap;
	private var __width:Number = 10;;
	private var __height:Number = 10;;
	public function TileRenderer() 
	{
		super();
	}
	
	public function get content():Bitmap { return _content; };
	
	public function set content(value:Bitmap):void 
	{
		_content = value;
		width = value.width;
		height = value.height;
		addChild(value);
	};
	
	override public function get data():Object { return super.data; };
	
	override public function set data(value:Object):void 
	{
		super.data = value;
		content = value as Bitmap;
	};
	
	override public function get width():Number { return __width; };
	
	override public function set width(value:Number):void 
	{
		__width = value;
	};
	
	override public function get height():Number { return __height; };
	
	override public function set height(value:Number):void 
	{
		__height = value;
	};
}